package com.ss.SmartStock.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ss.SmartStock.common.ErrorCode;
import com.ss.SmartStock.exception.BusinessException;
import com.ss.SmartStock.pojo.dto.product.ProductQueryAllRequest;
import com.ss.SmartStock.pojo.dto.product.ProductQueryRequest;
import com.ss.SmartStock.pojo.entity.Product;
import com.ss.SmartStock.mapper.ProductMapper;
import com.ss.SmartStock.pojo.entity.ProductDetails;
import com.ss.SmartStock.pojo.enums.StatusEnum;
import com.ss.SmartStock.pojo.vo.ProductQueryVO;
import com.ss.SmartStock.service.ProductDetailsService;
import com.ss.SmartStock.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ss.SmartStock.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-17
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductDetailsService productDetailsService;

//    @Resource
//    private ProductService productService;
    @Resource
    private UserService userService;




    @Override
    public List<Product> selectAllProduct() {
        return productMapper.selectAllProduct();
    }

    @Override
    public Product selectProductBypdid(Long id) {
        return productMapper.selectProductBypdid(id);
    }

    @Override
    public List<Product> selectAllProductById(Long id) {
        return productMapper.selectAllProductById(id);
    }

    /**
     * 获取查询包装类（用户根据哪些字段查询，根据前端传来的请求对象，得到 mybatis 框架支持的查询 QueryWrapper 类）
     *
     * @param productQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Product> getQueryWrapper(ProductQueryRequest productQueryRequest) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();

        if (productQueryRequest == null) {
            return queryWrapper;
        }

        Integer pDetailsId = productQueryRequest.getPDetailsId();
        String productName = productQueryRequest.getProductName();
        LocalDateTime timeOn = productQueryRequest.getTimeOn();
        LocalDateTime timeOff = productQueryRequest.getTimeOff();
        Integer productOnlineState = productQueryRequest.getProduct_online_state();

        Long operatorId = productQueryRequest.getOperator();

        //拼接查询条件
        queryWrapper.eq(ObjectUtils.isNotEmpty(pDetailsId), "p_details_id", pDetailsId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(productOnlineState), "product_online_state", productOnlineState);
        queryWrapper.like(StringUtils.isNotBlank(productName), "productName", productName);
        queryWrapper.like(ObjectUtils.isNotEmpty(operatorId), "operator", operatorId);
        queryWrapper.eq(timeOn != null, "timeOn", timeOn);
        queryWrapper.eq(timeOff != null, "timeOff", timeOff);


        return queryWrapper;
    }


    /**
     * 获取查询包装类VO
     *
     * @param productPage
     * @param httpServletRequest
     * @return
     */
    @Override
    public Page<ProductQueryVO> getProductVOPage(Page<Product> productPage, HttpServletRequest httpServletRequest) {

        List<Product> productList = productPage.getRecords();

        Page<ProductQueryVO> productQueryVOPage = new Page<>(productPage.getCurrent(), productPage.getSize(), productPage.getTotal());

        //叛空
        if (CollectionUtils.isEmpty(productList)) throw new BusinessException(ErrorCode.PARAMS_ERROR);

        // 填充信息
        List<ProductQueryVO> productQueryVOList = productList.stream().map((product) -> {
            ProductQueryVO productQueryVO = new ProductQueryVO();
            BeanUtils.copyProperties(product, productQueryVO);

            ProductDetails productDetails = productDetailsService.getById(product.getProductDetailsId());
            BeanUtils.copyProperties(productDetails, productQueryVO);

            return productQueryVO;

        }).collect(Collectors.toList());

        productQueryVOPage.setRecords(productQueryVOList);
        return productQueryVOPage;
    }


    @Override
    public Product getByDetailId(Long productDetailId) {

        return productMapper.getByDetailId(productDetailId);
    }

    @Override
    public void putProductOnline(long productId) {

//        Product product = productService.getByDetailId(productId);
        //切换状态-上线
//        product.setProductOnlineState(1);
    }

    @Override
    public void takeProductOffline(long productId) {
//        Product product = productService.getByDetailId(productId);
//        //切换状态-下线
//        product.setProductOnlineState(0);
    }

}
